package dwei.textwrap.wrapper
{
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import dwei.textwrap.WrapperAlign;

	/**
	 * 字符包装器，支持html显示文本
	 */	
	public class WrapperChar extends Wrapper
	{
//============================================================================
//PROPERTY
//============================================================================
		protected const CHAR_INDENT:int = 1;
		
		private var _charChanged:Boolean = false;
		protected var _char:String;
		
		private var _textFormatChanged:Boolean = false;
		protected var _textFormat:TextFormat;
		protected var _textField:TextField;
//============================================================================
//CONSTRUCTOR
//============================================================================
		public function WrapperChar()
		{
			super();
		}
//============================================================================
//OVERRIDE
//============================================================================
		override protected function createChildren():void
		{
			super.createChildren();
			_textField = new TextField();
			_textField.autoSize = TextFieldAutoSize.LEFT;
			addChild(_textField);
		}
		override protected function commitProperties():void
		{
			super.commitProperties();
			if (_charChanged)
			{
				_textField.htmlText = char;
				invalidateSize();
				_charChanged = false;
			}
			if (_textFormatChanged)
			{
				_textField.setTextFormat(_textFormat);
				invalidateSize();
				_textFormatChanged = true;
			}
		}
		override protected function measure():void
		{
			super.measure();
			measuredWidth = _textField.textWidth;// + CHAR_INDENT;
			measuredHeight = Math.max(_textField.textHeight, uint(_textFormat.size));
		}
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			_textField.x = _textField.textWidth - _textField.width + CHAR_INDENT;
			_textField.y = _textField.textHeight - _textField.height;
			switch (_verticalAlign)
			{
				case WrapperAlign.TOP:
					_textField.y += 0;
					break;
				case WrapperAlign.MIDDLE:
					_textField.y += (unscaledHeight - _textField.textHeight) / 2;
					break;
				default:
					_textField.y += unscaledHeight - _textField.textHeight;
					break;
			}
		}
//============================================================================
//GETTER/SETTER
//============================================================================
		public function set textFormat(value:TextFormat):void
		{
			if (value == _textFormat) return;
			_textFormat = value;
			_textFormatChanged = true;
			invalidateProperties();
		}
		
		public function get text():String
		{
			if (_char == null) return "";
			var tf:TextField = new TextField();
			tf.htmlText = _char;
			return tf.text;
		}
		
		public function get char():String
		{
			return _char;
		}

		public function set char(value:String):void
		{
			if (value == char) return;
			_char = value;
			_charChanged = true;
			invalidateProperties();
		}


	}
}